package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.base.domain.Letter;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.ITravelCommentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("travelComments")
public class TravelCommentController {

    @Autowired
    private ITravelCommentService travelCommentService;

    @Autowired
    private ITravelService travelService;

    /**
     * 获取当前游记的所有评论信息
     * @param travelId
     * @return
     */
    @GetMapping
    public JsonResult listComment(@RequestParam Long travelId){

        List<TravelComment> list =travelCommentService.queryByTravelId(travelId);
        return new JsonResult().setResult(list);
    }

    /**
     * 保存当前游记的评论
     * @param travelId
     * @param commentContent
     * @return
     */
    @PostMapping
    public JsonResult saveComment(@RequestParam Long travelId, @RequestParam String commentContent){
        JsonResult jsonResult = new JsonResult();

        TravelComment travelComment = new TravelComment();
        //设置用户
        User currentUser = UserContext.getCurrentUser();
        travelComment.setUser(currentUser);
        //设置游记
        Travel travel = travelService.queryByTravelId(travelId);
        travelComment.setTravel(travel);
        //设置状态
        travelComment.setStatus(TravelComment.UNREAD);
        //设置评论时间
        travelComment.setCommentTime(new Date());
        //设置评论内容
        travelComment.setCommentContent(commentContent);

        travelCommentService.save(travelComment);

        Travel travel1 = travelService.queryByTravelId(travelId);
        Integer commentCount = travel1.getCommentCount();
        if (commentCount==null){
            commentCount=0;
        }
        //游记评论数加一
        travelService.updateCommentCount(commentCount+1,travelId);

        return jsonResult;
    }
    /**
     * 根据评论ID 接受者ID查询  未读的信息  并 修改状态
     * @param authorId
     * @return
     */
    @PutMapping()
    public Object changeState(Long authorId){
         travelCommentService.updateState(authorId);
        return new JsonResult();
    }

    /**
     * 根据评论id查当前评论详细信息
     * @param travelCommentId
     * @return
     */
    @GetMapping("/{travelCommentId}")
    public JsonResult getByTravelCommentId(@PathVariable("travelCommentId") Long travelCommentId){
        JsonResult jsonResult = new JsonResult();
        try {
            TravelComment travelComment = travelCommentService.getByTravelCommentId(travelCommentId);
            return jsonResult.setResult(travelComment);
        }catch (Exception e){
            e.printStackTrace();
            return jsonResult.mark("出错了");
        }
    }

}
